package cn.com.ry.framework.mongodb.sync.util;

import cn.com.ry.framework.mongodb.sync.entity.PointBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * Created by wangrui on 2016/3/2.
 */
@Component
public class SynLogUtil {
    private static Logger logger = LoggerFactory.getLogger(SynLogUtil.class);

    public String path;

    private Properties prop = new Properties();

    //获取断点
    public PointBean getPoint() {
        PointBean pointBean = new PointBean();
        int maxPoint = 0;
        int minPoint = 0;
        List lastPointList = new ArrayList();
        if (prop != null) {
            Iterator it = prop.keySet().iterator();
            for (int i = 0; it.hasNext(); i++) {
                String key = (String) it.next();
                String logInfo = (String) prop.get(key);
                Assert.hasText(logInfo);
                String[] logInfos = logInfo.split(",");
                int queueIndex = Integer.parseInt(logInfos[0]);
                if (i == 0) {
                    minPoint = queueIndex;
                }

                if (queueIndex > maxPoint) {
                    maxPoint = queueIndex;
                }

                if (queueIndex < minPoint) {
                    minPoint = queueIndex;
                }
                String taskStatus = logInfos[4];
                if (taskStatus.equals("0")) {
                    lastPointList.add(queueIndex);
                }
            }
        }
        pointBean.setMaxPoint(maxPoint);
        pointBean.setMinPoint(minPoint);
        pointBean.setLastPointList(lastPointList);
        return pointBean;
    }

    public void setProperties(String path) {
        FileInputStream in = null;
        try {
            //没有就创建
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            in = new FileInputStream(file);
            prop.load(in);
            this.path = path;
        } catch (IOException e) {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            logger.error("[error]Could not load appConfiguration.properties", e);
        }
    }

    public Properties getProperties() {
        return prop;
    }

    /**
     * 修改或者新增key
     *
     * @param key
     * @param value
     */
    public void save(String key, String value) {
        prop.setProperty(key, value);
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(path);
            //将Properties中的属性列表（键和元素对）写入输出流
            prop.store(oFile, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getPropertyValue(String key) {
        return getPropertyValue(key, null);
    }

    public String getPropertyValue(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }

    public void save(String uri, String key, String value) {
        Properties properties = getProperties(uri);
        properties.setProperty(key, value);
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(uri);
            //将Properties中的属性列表（键和元素对）写入输出流
            properties.store(oFile, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getPropertyValue(String uri, String key, String defaultValue) {
        Properties properties = getProperties(uri);
        return properties.getProperty(key, defaultValue);
    }

    public Properties getProperties(String uri) {
        Properties properties=new Properties();
        FileInputStream in = null;
        try {
            //没有就创建
            File file = new File(uri);
            if (!file.exists()) {
                file.createNewFile();
            }
            in = new FileInputStream(file);
            properties.load(in);
        } catch (IOException e) {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            logger.error("[error]Could not load appConfiguration.properties", e);
        }
        return properties ;
    }

}
